var express = require('express');
var router = express.Router();

const {userModel} = require('../models/model');
router.post('/register', async (req, res) => {
    try {
        const {username, password} = req.body;
        if(!username || !password) {
            return res.send({code: 1, msg: '用户名和密码不能为空'});
        }
        const user = await userModel.findOne({username});
        if(user){
            return res.send({code: 1, msg: '用户已存在'});
        }
        const newUser = new userModel({username, password});
        await newUser.save();
        res.send({code: 0, msg: '注册成功'});
    } catch (error) {
        console.error('注册失败:', error);
        res.send({code: 500, msg: '服务器错误，请稍后再试'});
    }
})
module.exports = router;
